<template>
  <div id="entBalances">
    <div class="card">
      <div class="card-header bg-info text-white">企业客户授信</div>
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="公司名称"
              name="sc.name"
              v-model.trim="name"
              size="15"
            />
          </div>
          <div class="col-1">
            <select class="form-select ms-1" v-model.number="sc.pageSize">
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click="search()"
            >
              查找
            </button>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button
              type="button"
              class="btn btn-secondary btn-sm"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
      <table
        class="table table-response table-striped table-hover table-sm"
        id="tableList"
      >
        <thead>
          <tr>
            <th>被授权方</th>
            <th class="text-end">现金账户(元)</th>
            <th class="text-end">信用额度(元)</th>
            <th class="text-end">信用余额(元)</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(info, index) in dataList" :key="`ent_balance_` + info.id">
            <td>
              {{ info.name }}
              <span class="text-danger small">{{ info.customerCode }}</span>
            </td>
            <td class="text-end">
              {{ info.cashBalance / 100 }}
              <a
                href="javascript:void(0)"
                @click="editCashBalance(index)"
                class="small"
                >修改</a
              >
            </td>
            <td class="text-end">
              {{ info.creditLimit / 100 }}
              <a
                href="javascript:void(0)"
                @click="editCreditLimit(index)"
                class="small"
                >修改</a
              >
            </td>
            <td class="text-end">
              {{ info.creditBalance / 100 }}
            </td>
            <td>
              <router-link :to="`/customer/` + info.enterpriseId + `/trans`"
                >交易记录</router-link
              >
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-customer-balance ref="customerBalanceModal" :dataList="dataList">
    </my-modal-customer-balance>
    <my-modal-customer-cash-balance
      ref="customerCashBalanceModal"
      :dataList="dataList"
    >
    </my-modal-customer-cash-balance>
  </div>
</template>

<script>
import {
  searchCustomerBalances,
  saveEntCreditLimit,
  saveEntCashBalance,
} from "@/api/user.js";
import $ from "jquery";
import MyPagination from "@/components/my-pagination.vue";
import MyModalCustomerBalance from "@/components/modal/my-modal-customer-balance.vue";
import MyModalCustomerCashBalance from "@/components/modal/my-modal-customer-balance-cash.vue";

export default {
  components: {
    MyPagination,
    MyModalCustomerBalance,
    MyModalCustomerCashBalance,
  },
  data() {
    return {
      authorizee: 0,
      creditLimit: 0,
      opMode: 0,
      amount: 0,
      remark: "",

      dataList: [],
      name: "",
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
    };
  },
  mounted: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      console.log(this.$router);
      this.$router.replace("/h5/ent/balances");
    } else {
      this.search();
    }
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.name": this.name,
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };

      searchCustomerBalances(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    reset: function () {
      this.name = "";
    },
    editCreditLimit: function (index) {
      this.authorizee = this.dataList[index].enterpriseId;
      this.creditLimit = this.dataList[index].creditLimit / 100;

      this.$refs.customerBalanceModal
        .modal(this.authorizee, this.creditLimit)
        .then((v) => {
          this.saveCreditLimit(v);
        })
        .catch((ex) => {});
    },
    saveCreditLimit: function (v) {
      const params = {
        authorizee: v.authorizee,
        creditLimit: v.creditLimit,
      };

      saveEntCreditLimit(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败：" + jsonResult.errmsg);
        }
      });
    },
    editCashBalance: function (index) {
      this.authorizee = this.dataList[index].enterpriseId;

      this.$refs.customerCashBalanceModal
        .modal(this.authorizee)
        .then((v) => {
          this.updateCashBalance(v);
        })
        .catch((ex) => {});
    },
    updateCashBalance: function (v) {
      const params = {
        id: v.authorizee,
        opMode: v.opMode,
        modifyAmount: v.amount,
        remark: v.remark,
      };

      console.log(v);
      console.log(params);

      saveEntCashBalance(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg(jsonResult.errmsg);
        }
      });
    },
    getAuthorizeeName: function (enterpriseId) {
      for (let i = 0; i < this.customers.length; i++) {
        if (this.customers[i].enterpriseId === enterpriseId) {
          return this.customers[i].name;
        }
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
